package com.zhubjiept.app.jinrirong.activity.user;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.zhubjiept.app.R;
import com.zhubjiept.app.common.base.BaseMvpActivity;
import com.zhubjiept.app.common.widget.ProductRanking;
import com.zhubjiept.app.jinrirong.activity.user.presenter.MyInfoPresenter;
import com.zhubjiept.app.jinrirong.activity.user.view.RankingView;
import com.zhubjiept.app.jinrirong.config.ApiFactory;
import com.zhubjiept.app.jinrirong.config.UserManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wangsy on 2019/1/20.
 */

public class ProductRankingActivity extends BaseMvpActivity<RankingView, MyInfoPresenter> implements RankingView {

    @BindView(R.id.x_listView_2)
    RecyclerView mRvHelpList;


    @Override
    protected int setContentLayoutRes() {
        return R.layout.income_ranking_layout;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRvHelpList.setLayoutManager(new LinearLayoutManager(this));
        new Thread(() -> {
            mPresenter.requestProductRankingInfo(UserManager.getInstance().getToken());
        }).start();
    }

    @Override
    protected void initView() {
    }

    @Override
    protected MyInfoPresenter createPresenter() {
        return new MyInfoPresenter();
    }

    @Override
    public void showLoadingView() {
        super.showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onTokenInvalidate() {

    }

    @Override
    public void onNetworkConnectFailed() {

    }


    @Override
    public void onRemoteSucceed(JSONObject remoteResult) {
        JSONArray data = remoteResult.getJSONArray("data");
        List<ProductRanking> list = new ArrayList<>();
        if (data != null && data.size() > 0) {
            for (Object obj : data) {
                list.add(((JSONObject) obj).toJavaObject(ProductRanking.class));
            }
        }
        mRvHelpList.setAdapter(new ProductRankingViewAdapter(this, list));
    }

    @Override
    public void onRemoteFailed(String msg) {

    }

    class ProductRankingViewAdapter extends RecyclerView.Adapter<ProductRankingViewAdapter.ViewHolder> {

        private Context mContext;
        private LayoutInflater inflater;
        private List<ProductRanking> list;

        public ProductRankingViewAdapter(Context mContext, List<ProductRanking> list) {
            this.mContext = mContext;
            this.inflater = LayoutInflater.from(mContext);
            this.list = list;
        }


        @NonNull
        @Override
        public ProductRankingViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            ViewHolder holder = new ViewHolder(inflater.inflate(R.layout.ranking_item, viewGroup, false));
            //干掉两个元素
            holder.itemView.findViewById(R.id.ll_my_ranking).setVisibility(View.GONE);
            holder.itemView.findViewById(R.id.head_gift_logo).setVisibility(View.GONE);
            //去掉前三个
            ProductRanking num1 = list.remove(0);
            ProductRanking num2 = list.remove(0);
            ProductRanking num3 = list.remove(0);

            ((TextView) holder.itemView.findViewById(R.id.rank_number1_mobile)).setText(num1.getName());
            ((TextView) holder.itemView.findViewById(R.id.rank_number1_money)).setText(num1.getTotal());

            ((TextView) holder.itemView.findViewById(R.id.rank_number2_mobile)).setText(num2.getName());
            ((TextView) holder.itemView.findViewById(R.id.rank_number2_money)).setText(num2.getTotal());

            ((TextView) holder.itemView.findViewById(R.id.rank_number3_mobile)).setText(num3.getName());
            ((TextView) holder.itemView.findViewById(R.id.rank_number3_money)).setText(num3.getTotal());
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull ProductRankingViewAdapter.ViewHolder viewHolder, int i) {
            viewHolder.contentView.setLayoutManager(new LinearLayoutManager(mContext));
            viewHolder.contentView.setAdapter(new ProductRankingViewItemAdapter(mContext, list));
        }

        @Override
        public int getItemCount() {
            return 1;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            private RecyclerView contentView;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                contentView = itemView.findViewById(R.id.contentView);
            }
        }
    }

    class ProductRankingViewItemAdapter extends RecyclerView.Adapter<ProductRankingViewItemAdapter.ViewHolder> {
        private List<ProductRanking> rankings;
        private LayoutInflater inflater;
        String storePath = "/download/";

        public ProductRankingViewItemAdapter(Context mContext, List<ProductRanking> rankings) {
            this.inflater = LayoutInflater.from(mContext);
            this.rankings = rankings;
        }

        @NonNull
        @Override
        public ProductRankingViewItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new ProductRankingViewItemAdapter.ViewHolder(inflater.inflate(R.layout.two_item, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ProductRankingViewItemAdapter.ViewHolder holder, int i) {
            ((TextView) holder.itemView.findViewById(R.id.tv_ranking)).setText(String.valueOf((i + 4)));
            ((TextView) holder.itemView.findViewById(R.id.account_mobile)).setText(rankings.get(i).getName());
            ((TextView) holder.itemView.findViewById(R.id.total_money)).setText(rankings.get(i).getTotal());
            String img = String.valueOf(rankings.get(i).getLogurl());

            if (!("".equals(img) || "null".equals(img))) {
                Glide.with(holder.itemView.getContext()).load(ApiFactory.RESOURCE_URL + img).into(((ImageView) holder.itemView.findViewById(R.id.headerImage)));
            } else {
                ((ImageView) holder.itemView.findViewById(R.id.headerImage)).setImageResource(R.drawable.link_icon);
            }

        }

        @Override
        public int getItemCount() {
            return rankings.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
            }
        }

        public Bitmap getHttpBitmap(String url) {
            Bitmap bitmap = null;
            try {
                URL pictureUrl = new URL(url);
                InputStream in = pictureUrl.openStream();
                bitmap = BitmapFactory.decodeStream(in);
                in.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return bitmap;
        }

        public void savePicture(Bitmap bitmap, String pic_name) {
            String pictureName = Environment.getExternalStorageDirectory().getAbsolutePath() + storePath + pic_name;
            File file = new File(pictureName);
            FileOutputStream out;
            try {
                out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("文件路径：" + file.getAbsolutePath());
        }
    }
}